package Control;

import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JOptionPane;

import Entity.Activity_from_server;
import Entity.MsgPackage;
import Entity.Parformance_report;
import Entity.TardinessReport_ENTITY;
import Entity.Tardiness_report_in_array_ENTITY;
import GUI.ActivityRepChart_GUI;
//import GUI.ActivityRepChart_GUI;
import GUI.ActivityReportOutput_Gui;
import GUI.ParformanceRep_GUI;
import GUI.Report_GUI;
import GUI.ScreenManager;
import GUI.TardinessReport_GUI;
import Entity.ActivityReport;


public class Report_Control {
	
	private ActivityReportOutput_Gui G_ARoutput;
	private Report_GUI rep_gui;
	private MsgPackage rep_msg;
	private String start_date;
	private String end_date;
	private ActivityReport AR_e;
	private ActivityRepChart_GUI activGraph_gui;
	private TardinessReport_GUI TRG_output;
	private TardinessReport_ENTITY TR;
	private Tardiness_report_in_array_ENTITY TROIA_e;
	
	private Parformance_report PR;
    private ParformanceRep_GUI PR_output;
	
	public Report_Control(){
	TRG_output = new TardinessReport_GUI ();
	TR = new TardinessReport_ENTITY();
	TROIA_e = new Tardiness_report_in_array_ENTITY();
	
		rep_gui = new Report_GUI();
		rep_msg = new MsgPackage();
		AR_e = new ActivityReport();
		G_ARoutput = new ActivityReportOutput_Gui();
        PR = new Parformance_report();
        PR_output = new ParformanceRep_GUI();



	}
	
	public Report_GUI getrep_gui(){
		return rep_gui;
	}

	
	public boolean ActivityReport_pressed(){
		start_date = rep_gui. getStartDay_textField().getText();
		if (start_date == null)
		{
			
			JOptionPane.showMessageDialog(null, "Start Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);

             return false;
		}
		end_date = rep_gui.getEndDate_textField_1().getText();
		if(end_date == null)
		{
			JOptionPane.showMessageDialog(null, "End Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);
		        return false;	
	    }
		else{
			
			AR_e.setFrom(start_date);
			AR_e.setTo(end_date);
			
			        
		        	rep_msg.setObject(AR_e); 
			        rep_msg.setOp_code(OP_CODE.ACTIVITY_REPORT);//get the op-code to know which query to active
			        
			        ScreenManager.getConnection().accept(rep_msg);//send the msg to the server
			    	Object o = ScreenManager.getConnection().cclient.getMessage();
			    	ActivityReport AR1 = (ActivityReport) o;
			    	fillActivityReport(AR1);
		            return true;
			}
		}
		/**
		 * 
		 * @param 
		 * @return
		 */
	public boolean TardinessReport_pressed(){
	 	
		start_date = rep_gui. getStartDay_textField().getText();
		if (start_date == null)
		{
			
			JOptionPane.showMessageDialog(null, "Start Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);

             return false;
		}
		end_date = rep_gui.getEndDate_textField_1().getText();
		if(end_date == null)
		{
			JOptionPane.showMessageDialog(null, "End Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);
		        return false;	
	    }
		else{
		TR.setFrom(start_date);
		TR.setTo(end_date);
		
		
		
		rep_msg.setObject(TR); //convert the start and end date to object
		rep_msg.setOp_code(OP_CODE.TARDINESS_REP);//get the op-code to know which query to active
		        ScreenManager.getConnection().accept(rep_msg);//send the msg to the server
		    	Object o = ScreenManager.getConnection().cclient.getMessage();
		    	TardinessReport_ENTITY TR2 = (TardinessReport_ENTITY) o; 
		    	System.out.println(TR2.getNumber_late_requests());
		        System.out.println(TR2.getSum_late_requests());
		    	fillTardinessReport(TR2);
		        
	            return true;
		}
		
    	
	}
		
		public boolean parformance_pressed(){
			start_date = rep_gui. getStartDay_textField().getText();
			if (start_date == null)
			{
				
				JOptionPane.showMessageDialog(null, "Start Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);

	             return false;
			}
			end_date = rep_gui.getEndDate_textField_1().getText();
			if(end_date == null)
			{
				JOptionPane.showMessageDialog(null, "End Date is missing", "ERROR", JOptionPane.ERROR_MESSAGE);
			        return false;	
		    }
			else{
			PR.setFrom(start_date);
			PR.setTo(end_date);
			
			
			
			rep_msg.setObject(PR); //convert the start and end date to object
			rep_msg.setOp_code(OP_CODE.PARFORMANCE_REP);//get the op-code to know which query to active
			        ScreenManager.getConnection().accept(rep_msg);//send the msg to the server
			    	Object o = ScreenManager.getConnection().cclient.getMessage();
			    	Parformance_report PR2 = (Parformance_report) o; 
			    	fillParformanceReport(PR2);
			        
		            return true;
			
			}
		}
	
	   	private void fillParformanceReport(Parformance_report pR2) {
	   		System.out.println(pR2.getSumExtendedApp());
	   		System.out.println(pR2.getSumReturnStageDur());

			PR_output.getAppDay().setText(Integer.toString(pR2.getSumExtendedApp()));
			PR_output.getAddDay().setText(Integer.toString(pR2.getSumReturnStageDur()));
			
			
		}

		private void fillTardinessReport(TardinessReport_ENTITY tR2) {
	   		TRG_output.getTable().setValueAt(FrequencyDistribution(tR2.getSum_late_requests()),2 , 2);
			TRG_output.getTable().setValueAt(FrequencyDistribution(tR2.getNumber_late_requests()),1 , 2);
			
			
			
			
			TRG_output.getTable().setValueAt(median(tR2.getSum_late_requests()), 2, 4);
			TRG_output.getTable().setValueAt(median(tR2.getNumber_late_requests()),1 , 4);
			

			TRG_output.getTable().setValueAt(StandardDeviation(tR2.getSum_late_requests()), 2, 3);
			TRG_output.getTable().setValueAt(StandardDeviation(tR2.getNumber_late_requests()),1 , 3);
			
			
		}


		public void fillActivityReport(ActivityReport AR) {
			/*
	   		System.out.println("activ");
	   		System.out.println(AR.getActiveReqList());
	   		System.out.println("frozen");
	   		System.out.println(AR.getFrozenReqList());
	   		System.out.println("closed");
	   		System.out.println(AR.getClosedReqList());
	   		System.out.println("reject");
	   		System.out.println(AR.getRejectReqList());
	   		*/
         	G_ARoutput.getTable_1().setValueAt(FrequencyDistribution(AR.getActiveReqList()), 1, 2);
        	G_ARoutput.getTable_1().setValueAt(FrequencyDistribution(AR.getFrozenReqList()), 2, 2);
        	G_ARoutput.getTable_1().setValueAt(FrequencyDistribution(AR.getClosedReqList()), 3, 2);
        	G_ARoutput.getTable_1().setValueAt(FrequencyDistribution(AR.getRejectReqList()), 4, 2);
		    
        	G_ARoutput.getTable_1().setValueAt(StandardDeviation(AR.getActiveReqList()), 1, 3);
        	G_ARoutput.getTable_1().setValueAt(StandardDeviation(AR.getFrozenReqList()), 2, 3);
        	G_ARoutput.getTable_1().setValueAt(StandardDeviation(AR.getClosedReqList()), 3, 3);
        	G_ARoutput.getTable_1().setValueAt(StandardDeviation(AR.getRejectReqList()), 4, 3);
        	
        	G_ARoutput.getTable_1().setValueAt(median(AR.getActiveReqList()), 1, 4);
        	G_ARoutput.getTable_1().setValueAt(median(AR.getFrozenReqList()), 2, 4);
        	G_ARoutput.getTable_1().setValueAt(median(AR.getClosedReqList()), 3, 4);
        	G_ARoutput.getTable_1().setValueAt(median(AR.getRejectReqList()), 4, 4);
        	
        	G_ARoutput.getTable_1().setValueAt(AR.getActiveReqDuration(),1,1);
        	G_ARoutput.getTable_1().setValueAt(AR.getFrozenReqDuration(),2,1);
        	G_ARoutput.getTable_1().setValueAt(AR.getClosedReqDuration(),3,1);
        	G_ARoutput.getTable_1().setValueAt(AR.getRejectReqDuration(),4,1);
	}
		
		//*******************************************************************************************
		/**
		 * This class makes all calculation needed
		 * to display on one of the report's panel
		 *  
		 */
	
	/**
	 * 
	 * @param med the arr that will returned from the data base white all the data
	 * @return the median of the data
	 */
		
	public Activity_from_server	graph_was_pressed(){
		
        String[] dates=new String[2];
        dates[0]=start_date;
        dates[1]=end_date;
        
        	rep_msg.setObject(dates); //convert the start and end date to object
		rep_msg.setOp_code(OP_CODE.ACTIVITY_GRAPH);//get the op-code to know which query to active
		        ScreenManager.getConnection().accept(rep_msg);//send the msg to the server
		    	Object o = ScreenManager.getConnection().cclient.getMessage();
		        Activity_from_server ac=(Activity_from_server) o;
		        return ac;
		
			
		}
	
	public float median(ArrayList<Integer> med) 
	{

	    float middle = (float) (med.size()/2.0); 
	  		
	  		//sort the list
	  		Collections.sort(med);
	   

	    if (middle%2 == 1) 
	    {
	        return med.get((int)middle);
	    } 
	    else 
	    {
	        return (float) ((med.get((int)middle) + med.get((int)middle+1) ) / 2.0);
	    }
	}
	/**
	 * 
	 * @param  avr array list that hold the  data from the data base
	 * @return the avrage of the avr 
	 */
	
	public float avrage(ArrayList<Integer> avr) 
	{
	    int sum = 0;
	    
	    for (int i = 0; i < avr.size(); i++) 
	    	
	            sum = sum + avr.get(i);
	    
	    return (float)sum / avr.size();
	}
	public double incassoMargherita(ArrayList<Integer> m )
	{   
		ArrayList<Integer> y = m;
	    double sum = 0;
	    for(int i = 0; i < y.size(); i++)
	    {
	        sum += m.get(i);
	    }
	    System.out.println(sum);
	    return sum;
	}
	
	/**
	 * 
	 * @param sd array list that hold the data from the data base
	 * @return the standardfevition of the data
	 */
	public float StandardDeviation(ArrayList<Integer>sd){
		
		float num = (float) (1.0/sd.size()); 
	
		
		float avre,temp=0;
		
		avre = (float) avrage(sd);
		
		for(int i=0;i<sd.size();i++)
		{
			temp = (float) (temp + Math.pow(sd.get(i)- avre,2));
		}
		
	
		return (float) Math.sqrt(num*temp) ;
		
	}	
	
	/**
	 * 
	 * @param fdis array list that hold the data from the data base
	 * @return Frequency Distribution of the data
	 */
    public double FrequencyDistribution(ArrayList<Integer>fdis){
		
		int sum = 0;
		for( int i=0 ;i<fdis.size();i++) {
		    sum = sum + fdis.get(i);
		}

		
		return (sum);
		
	}



	
	public Report_GUI getRep_gui() {
		return rep_gui;
	}

	public void setRep_gui(Report_GUI rep_gui) {
		this.rep_gui = rep_gui;
	}

	public MsgPackage getRep_msg() {
		return rep_msg;
	}

	public void setRep_msg(MsgPackage rep_msg) {
		this.rep_msg = rep_msg;
	}


	public ActivityReportOutput_Gui getG_ARoutput() {
		return G_ARoutput;
	}


	public void setG_ARoutput(ActivityReportOutput_Gui g_ARoutput) {
		G_ARoutput = g_ARoutput;
	}


	public String getStart_date() {
		return start_date;
	}


	public void setStart_date(String start_date) {
		this.start_date = start_date;
	}


	public String getEnd_date() {
		return end_date;
	}


	public Parformance_report getPR() {
		return PR;
	}

	public void setPR(Parformance_report pR) {
		PR = pR;
	}

	public ParformanceRep_GUI getPR_output() {
		return PR_output;
	}

	public void setPR_output(ParformanceRep_GUI pR_output) {
		PR_output = pR_output;
	}

	public void setEnd_date(String end_date) {
		this.end_date = end_date;
	}


	public ActivityReport getAR_e() {
		return AR_e;
	}


	public void setAR_e(ActivityReport aR_e) {
		AR_e = aR_e;
	}


	public ActivityRepChart_GUI getActivGraph_gui() {
		return activGraph_gui;
	}


	public void setActivGraph_gui(ActivityRepChart_GUI activGraph_gui) {
		this.activGraph_gui = activGraph_gui;
	}


	public TardinessReport_GUI getTRG_output() {
		return TRG_output;
	}


	public void setTRG_output(TardinessReport_GUI tRG_output) {
		TRG_output = tRG_output;
	}


	public TardinessReport_ENTITY getTR() {
		return TR;
	}


	public void setTR(TardinessReport_ENTITY tR) {
		TR = tR;
	}


	public Tardiness_report_in_array_ENTITY getTROIA_e() {
		return TROIA_e;
	}


	public void setTROIA_e(Tardiness_report_in_array_ENTITY tROIA_e) {
		TROIA_e = tROIA_e;
	}

}
